import pandas as pd
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from nltk.sentiment import SentimentIntensityAnalyzer
from nltk.probability import FreqDist
from deep_translator import GoogleTranslator
import time
nltk.download('punkt')
nltk.download('stopwords')
nltk.download('wordnet')
nltk.download('vader_lexicon')

df = pd.read_csv('评论数据 1005.csv')

# 初始化翻译器
translator = GoogleTranslator(source='auto', target='en')


# 提取正面/负面评论的核心关键词
# 1. 文本预处理（分词、过滤、词形还原）
def preprocess_text(text):
    # 分词（将句子拆分为单词）
    tokens = word_tokenize(text.lower())  # 转为小写后分词

    # 过滤停用词（如"the"、"is"）和非字母字符
    stop_words = set(stopwords.words('english'))
    filtered_tokens = [token for token in tokens
                       if token.isalpha()  # 只保留字母
                       and token not in stop_words  # 排除停用词
                       and len(token) > 2]  # 排除短词（如"is"、"it"）

    # 词形还原（统一词汇形态，如"amazing"→"amaze"，"charging"→"charge"）
    lemmatizer = WordNetLemmatizer()
    lemmatized_tokens = [lemmatizer.lemmatize(token) for token in filtered_tokens]

    return lemmatized_tokens

# 2. 情感分析（判断评论正负倾向）
def analyze_sentiment(text):
    sia = SentimentIntensityAnalyzer()
    # 输出负面(neg)、中性(neu)、正面(pos)、综合(compound)得分
    sentiment_scores = sia.polarity_scores(text)
    return sentiment_scores

# 3. 关键词提取（统计高频词）
def extract_keywords(tokens_list, top_k=5):
    # 合并所有词汇并统计词频
    all_tokens = [token for tokens in tokens_list for token in tokens]
    freq_dist = FreqDist(all_tokens)
    return freq_dist.most_common(top_k)

sentiment_results = []
# 遍历comment列并翻译
for idx, comment in enumerate(df['comment']):
    if str(comment) is None:
        print("数据读取为空或失败")
        continue

    translated = translator.translate(str(comment))

    if len(comment) < 2:
        continue
    processed_tokens = preprocess_text(translated)
    score = analyze_sentiment(translated)
    score = round(score, 2)
    # 自定义情感标签（根据业务调整阈值，这里用0.6为正面，0.4为负面）
    if score >= 0.6:
        label = "正面"
    elif score <= 0.4:
        label = "负面"
    else:
        label = "中性"
    print({"评论": translated, "情感得分": score, "标签": label})
    sentiment_results.append({"评论": translated, "情感得分": score, "标签": label})

positive_texts = [res["评论"] for res in sentiment_results if res["标签"] == "正面"]
positive_keywords = extract_keywords(positive_texts)
print("正面评论核心关键词：", positive_keywords)
